package com.srpingde.hhh;

import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nrs.utils.tools.CrashHandler;
import com.springde.hhh.R;
import com.srpingde.hhh.adapter.MeiTuAdapter;
import com.srpingde.hhh.adapter.HHHFenLeiAdapter;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.application.HHHDataManage;
import com.srpingde.hhh.bean.MeiWenBean;
import com.srpingde.hhh.bean.MeiWenDataBean;
import com.srpingde.hhh.bean.HHHMeiTu;
import com.srpingde.hhh.bean.YouXiBean;
import com.srpingde.hhh.bean.YouXiDataBean;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class HHHFenLeiActivity extends Activity {
	ListView listView;
	ImageView back;
	TextView top_title;
	String id = null;
	TextView top_title_tv;
	MeiWenBean meiWenBean;
	YouXiBean youXiBean;
	HHHMeiTu weiQuMeiTu;
	/*List<WeiQuFenLeiBean> WeiQuFenLeiBean;
	ArrayList<WeiQuFenLeiBean> list;*/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_qu_fen_lei);
        CrashHandler crashHandler = CrashHandler.getInstance() ;
		crashHandler.init(this);
        layout();
        listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if(id.equals("4")){
					Intent in = new Intent(HHHFenLeiActivity.this,HHHFenLeiXiangQingActivity.class);
					in.putExtra("content", weiQuMeiTu.getRows().get(arg2));
					startActivity(in);
				}else if(id.equals("1")){
					if(!youXiBean.getRows().get(arg2).getApk().contains("http://")){
						Toast.makeText(getApplicationContext(), "抱歉，后台数据添加错误，请选择另外游戏进行下载，我们会尽快修正这个错误", Toast.LENGTH_LONG).show();
					}else{
						Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse(youXiBean.getRows().get(arg2).getApk()));
				        startActivity(it);
					}
				}else{
					Intent in = new Intent(HHHFenLeiActivity.this,MeiWenActivity.class);
					in.putExtra("id", id);
					in.putExtra("aid", meiWenBean.getRows().get(arg2).getAid());
					in.putExtra("pid",  meiWenBean.getRows().get(arg2).getPcid());
					in.putExtra("title", meiWenBean.getRows().get(arg2).getTitle());
					startActivity(in);
				}
			}
		});
//        gridView.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
//					long arg3) {
//				// TODO Auto-generated method stub
//				Intent in = new Intent(WeiQuFenLeiActivity.this,WiQuFenLeiXiangQingActivity.class);
//				in.putExtra("content", meiTuBean.getRows().get(arg2));
//				startActivity(in);
//			}
//		});
        if(getIntent().getExtras()!=null){
        	id = getIntent().getExtras().getString("id");
        	if(id.equals("1") || id.equals("2") || id.equals("3") ){
        		if(id.equals("1")){
        			top_title_tv.setText("游戏天地");
        			getYouXiData();
        			return;
        		}
        		if(id.equals("2")){
        			top_title_tv.setText("技术分享");
        			getData(HHHInterface.httpHeader + "article.php?act=date&acat_keyword=technology");
        			return;
        		}
        		if(id.equals("3")){
        			top_title_tv.setText("美文赏析");
        			getData(HHHInterface.httpHeader + "article.php?act=date&acat_keyword=literature");
        			return;
        		}
        	}else if(id.equals("4")){
        		top_title_tv.setText("美图赏析");
        		getMeiTuData(id);
        		return;
        	}
        }
    }
    public void onClick(View v){
    	finish();
    }
    private void getData(String cid){
    	HHHIml weiQuIml = new HHHIml();
    	weiQuIml.weiQuTianDiListHttp(HHHFenLeiActivity.this, cid, new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						Log.e("mytag","得到的数据"+content);
						meiWenBean=new MeiWenBean();
						ArrayList<MeiWenDataBean> rows=new ArrayList<MeiWenDataBean>();
						try {
							JSONObject json=new JSONObject(content);
							JSONArray jsons=new JSONArray(json.getString("rows"));
							for (int i = 0; i < jsons.length(); i++) {
								JSONObject rowsobject= jsons.getJSONObject(i);
								MeiWenDataBean meiwen=new MeiWenDataBean();
								meiwen.setAid(rowsobject.getString("aid"));
								meiwen.setCid(rowsobject.getString("cid"));
								meiwen.setCname(rowsobject.getString("cname"));
								meiwen.setContent(rowsobject.getString("content"));
								meiwen.setDate(rowsobject.getString("date"));
								meiwen.setDatetime(rowsobject.getString("datetime"));
								meiwen.setId(rowsobject.getString("id"));
								meiwen.setOrderid(rowsobject.getString("orderid"));
								meiwen.setPcid(rowsobject.getString("pcid"));
								meiwen.setTitle(rowsobject.getString("title"));
								rows.add(meiwen);
							}
						
						} catch (JSONException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						meiWenBean.setRows(rows);
						listView.setAdapter(new HHHFenLeiAdapter(HHHFenLeiActivity.this,meiWenBean.getRows()));
					}
		});
    	
    }
    private void getYouXiData(){
    	String url = HHHInterface.httpHeader + "api.php?tab=game&act=data&pagesize=10&page=1";
    	HHHIml weiQuIml = new HHHIml();
    	weiQuIml.weiQuTianYOUxIDiListHttp(HHHFenLeiActivity.this, url, new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						Log.e("mytag","得到的数据"+content);
						youXiBean = HHHDataManage.getInstance().getYouXiBean();
						listView.setAdapter(new YouxiAdapter(youXiBean.getRows(),HHHFenLeiActivity.this));
					}
		});
    	
    }
    private void getMeiTuData(String cid){
    	String url = HHHInterface.httpHeader + "api.php?tab=acat&act=data&pagesize=10&page=1&pid=4";
    	HHHIml weiQuIml = new HHHIml();
    	weiQuIml.meiTuShangXiHttp(HHHFenLeiActivity.this, url, new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						Log.e("mytag", content + "========唯趣天地========");
						weiQuMeiTu = HHHDataManage.getInstance().getQuMeiTu();
						listView.setAdapter(new MeiTuAdapter(weiQuMeiTu.getRows(),HHHFenLeiActivity.this));
						
					}
		});
    }
    private void layout() {
    	listView = (ListView) findViewById(R.id.weiqufenlei_lv);
        back = (ImageView) findViewById(R.id.back);
        top_title = (TextView) findViewById(R.id.top_title);
        //list = new ArrayList<WeiQuFenLeiBean>();
		// 头的高度
		top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv
				.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		//back
		RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
	}
    public class YouxiAdapter extends BaseAdapter{
    	ArrayList<YouXiDataBean> youXiDataBeans = new ArrayList<YouXiDataBean>();
    	Context context;
    	
		public YouxiAdapter(ArrayList<YouXiDataBean> youXiDataBeans,
				Context context) {
			super();
			this.youXiDataBeans = youXiDataBeans;
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return youXiDataBeans.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			convertView = LayoutInflater.from(context).inflate(R.layout.youxi_item, null);
			ImageView imageView = (ImageView) convertView.findViewById(R.id.youxi_item_iv);
			TextView miaoshu = (TextView) convertView.findViewById(R.id.youxi_item_miaoshu);
			//TextView link = (TextView) convertView.findViewById(R.id.youxi_item_link);
			TextView youxi_item_link = (TextView) convertView.findViewById(R.id.youxi_item_link);
			final ImageView kvShow = imageView;
    		ImageLoader.getInstance().loadImage(HHHIml.httpHeader+youXiDataBeans.get(position).getPic(), new SimpleImageLoadingListener(){
    			@Override
    			public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
    				BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
    				//kvShow.setImageBitmap(bitmap);
    				kvShow.setBackgroundDrawable(bitmapDrawable);
    			}
    		});
    		miaoshu.setText(youXiDataBeans.get(position).getDesc());
    		//link.setText(youXiDataBeans.get(position).getApk());
    		itemLayout(imageView);
    		textviewLayout(youxi_item_link);
			return convertView;
		}
		private void itemLayout(ImageView imageView){
			RelativeLayout.LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
			layoutParams.height = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[0]*0.13);
			layoutParams.width = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1]*0.2);
			imageView.setLayoutParams(layoutParams);
		}
		private void textviewLayout(TextView imageView){
			RelativeLayout.LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
			layoutParams.height = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[0]*0.05);
			layoutParams.width = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1]*0.1);
			imageView.setLayoutParams(layoutParams);
		}
    }
}
